package apk.yoursms.database;

import java.util.HashMap;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;

public class SmsProvider extends ContentProvider {

    public static final String DB_SMSBOX = "smsbox.db";
    public static final Uri CONTENT_URI = Uri
	    .parse("content://apk.yoursms.database.SmsProvider/smsbox");
    public static final Uri CONTENT_URI_EN = Uri
	    .parse("content://apk.yoursms.database.SmsProvider/smsbox_en");
    public static final int URI_CODE = 1;
    public static final int URI_CODE_ID = 2;
    public static final int URI_CODE_EN = 3;
    public static final int URI_CODE_ID_EN = 4;   
    public static final int URI_CODE_LIST_RU = 5;
    public static final int URI_CODE_ID_LIST_RU = 6;
    public static final int URI_CODE_LIST_EN = 7;
    public static final int URI_CODE_ID_LIST_EN = 8;

    private static UriMatcher mUriMatcher;
    private static HashMap<String, String> mSmsMap;
    private SQLiteDatabase db;

    static {
	mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
	mUriMatcher.addURI("apk.yoursms.database.SmsProvider",
		SmsDbHelper.TABLE_NAME, URI_CODE);
	mUriMatcher.addURI("apk.yoursms.database.SmsProvider",
		SmsDbHelper.TABLE_NAME + "/#", URI_CODE_ID);
	mUriMatcher.addURI("apk.yoursms.database.SmsProvider",
		SmsDbHelper.TABLE_NAME_EN, URI_CODE_EN);
	mUriMatcher.addURI("apk.yoursms.database.SmsProvider",
		SmsDbHelper.TABLE_NAME_EN + "/#", URI_CODE_ID_EN);

	mSmsMap = new HashMap<String, String>();
	mSmsMap.put(SmsDbHelper._ID, SmsDbHelper._ID);
	mSmsMap.put(SmsDbHelper.GROUP, SmsDbHelper.GROUP);
	mSmsMap.put(SmsDbHelper.SMS_TEXT, SmsDbHelper.SMS_TEXT);
    }

    public String getDbName() {
	return (DB_SMSBOX);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
	int retVal = 0;
	switch (mUriMatcher.match(uri)) {
	case URI_CODE:
	    retVal = db
		    .delete(SmsDbHelper.TABLE_NAME, selection, selectionArgs);
	    getContext().getContentResolver().notifyChange(uri, null);
	    break;

	case URI_CODE_EN:
	    retVal = db.delete(SmsDbHelper.TABLE_NAME_EN, selection,
		    selectionArgs);
	    getContext().getContentResolver().notifyChange(uri, null);
	    break;
	}

	return retVal;
    }

    @Override
    public String getType(Uri uri) {
	// TODO Auto-generated method stub
	return null;
    }

    @Override
    public Uri insert(Uri url, ContentValues inValues) {
	ContentValues values = new ContentValues(inValues);
	Uri uri = null;
	switch (mUriMatcher.match(url)) {
	case URI_CODE:
	    long rowId = db.insert(SmsDbHelper.TABLE_NAME, SmsDbHelper.GROUP,
		    values);
	    if (rowId > 0) {
		uri = ContentUris.withAppendedId(CONTENT_URI, rowId);
		getContext().getContentResolver().notifyChange(uri, null);
	    } else
		throw new SQLException("Failed to insert row into " + url);
	    break;

	case URI_CODE_EN:
	    long rowIdEn = db.insert(SmsDbHelper.TABLE_NAME_EN,
		    SmsDbHelper.GROUP, values);
	    if (rowIdEn > 0) {
		uri = ContentUris.withAppendedId(CONTENT_URI_EN, rowIdEn);
		getContext().getContentResolver().notifyChange(uri, null);
	    } else
		throw new SQLException("Failed to insert row into " + url);
	    break;
	}

	return uri;

    }

    @Override
    public boolean onCreate() {
	// TODO Auto-generated method stub	
	 db = (new SmsDbHelper(getContext())).getWritableDatabase();
	 return (db == null) ? false : true;	
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
	    String[] selectionArgs, String sortOrder) {
	String orderBy;
	if (TextUtils.isEmpty(sortOrder))
	    orderBy = SmsDbHelper.GROUP;
	else
	    orderBy = sortOrder;

	Cursor c = null;
	switch (mUriMatcher.match(uri)) {
	case URI_CODE:
	    c = db.query(SmsDbHelper.TABLE_NAME, projection, selection,
		    selectionArgs, null, null, orderBy);
	    break;

	case URI_CODE_EN:
	    c = db.query(SmsDbHelper.TABLE_NAME_EN, projection, selection,
		    selectionArgs, null, null, orderBy);
	    break;
	}

	c.setNotificationUri(getContext().getContentResolver(), uri);
	return c;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
	    String[] selectionArgs) {
	int retVal = 0;
	switch (mUriMatcher.match(uri)) {
	case URI_CODE:
	    retVal = db.update(SmsDbHelper.TABLE_NAME, values, selection,
		    selectionArgs);
	    break;

	case URI_CODE_EN:
	    retVal = db.update(SmsDbHelper.TABLE_NAME_EN, values, selection,
		    selectionArgs);
	    break;
	}

	getContext().getContentResolver().notifyChange(uri, null);
	return retVal;
    }

}
